package com.fire.user.util;

import java.io.IOException;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fire.core.jkmodel.ReqDTO;
import com.fire.core.jkmodel.XmlMessageDTO;
import com.fire.core.service.ITMitServiceDicBaseServices;
import com.fire.core.util.BlogUtil;
import com.fire.core.util.HttpUtils;
import com.fire.core.util.XmlMessageUtil;
import com.fire.core.util.XmlUtil;
import com.fire.user.constants.JkConstants;
import com.fire.user.model.TMitServiceDicBase;
import com.fire.user.service.ITMitServiceDicBaseService;

/**
 * 类描述：
 *
 * @author Aspire
 * @version 1.0
 */
@Component
public class JkUtils {
    private static Logger logger = Logger.getLogger(JkUtils.class);

    @Autowired
    private ITMitServiceDicBaseService baseService;
    @Autowired
    private ITMitServiceDicBaseServices baseServices;

    private  String PARTNER_CODE = "";
    private  String URL = "";
    private  String MD5_KEY = "";

    /**
     * 获取配置信息
     * */
    public void getConfigure(){
        MD5_KEY = baseServices.getBaseByBkey(JkConstants.JK_MD5_KEY);
        URL = baseServices.getBaseByBkey(JkConstants.JK_SERVICE_URL);
        PARTNER_CODE = baseServices.getBaseByBkey(JkConstants.JK_PARTNER_CODE);
        /*TMitServiceDicBase baseInfo = baseService.selectById(JkConstants.JK_MD5_KEY);
        if (baseInfo!=null){
            MD5_KEY= baseInfo.getBvalue().trim();
        }

        TMitServiceDicBase baseInfo1 = baseService.selectById(JkConstants.JK_SERVICE_URL);
        if (baseInfo1!=null){
            URL=baseInfo1.getBvalue().trim();
        }

        TMitServiceDicBase baseInfo2 = baseService.selectById(JkConstants.JK_PARTNER_CODE);
        if (baseInfo2!=null){
            PARTNER_CODE=baseInfo2.getBvalue().trim();
        }*/

    }

    /**
     * 检查签名是否合法
     *
     * @param xmlMessageDTO
     * @return
     */
    public boolean checkSign(XmlMessageDTO xmlMessageDTO) {
        getConfigure();
        String sign = DigestUtils.md5Hex(MD5_KEY + xmlMessageDTO.getContent());
        logger.info("接收的签名:" + xmlMessageDTO.getSign());
        logger.info("生成的签名:" + sign);
        if (sign.equals(xmlMessageDTO.getSign())) {
            logger.info("签名校验成功");
            return true;
        }
        logger.info("签名校验失败");
        return false;
    }

    /**
     * 提交请求
     *
     * @param reqDTO
     *            请求信息
     * @return 响应内容
     * @throws JDOMException
     * @throws IOException
     */
    public  String postRequest(ReqDTO<?> reqDTO,String who,String where,String what,String attach) throws Exception {
        getConfigure();
        String content = JSON.toJSONString(reqDTO);
        String sign = DigestUtils.md5Hex(MD5_KEY + content);
        String postXmlStr = XmlMessageUtil.createXml(PARTNER_CODE, sign, content);
        logger.debug("请求的消息" + postXmlStr);
        logger.info("请求的消息" + postXmlStr);
        JSONObject json=XmlUtil.xml2JSON(postXmlStr);
        BlogUtil.outlog(json.toJSONString(), who, where, what, "req",attach);
        String responseStr = null;
        try {
            responseStr = HttpUtils.request(postXmlStr, URL, 10 * 1000);
            JSONObject jsonResp=XmlUtil.xml2JSON(responseStr);
            BlogUtil.outlog(jsonResp.toJSONString(), who, where, what, "resp",attach);
            logger.debug("返回消息" + responseStr);
            logger.info("返回消息" + responseStr);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return responseStr;
    }


    public String getPARTNER_CODE() {
        return PARTNER_CODE;
    }

    public void setPARTNER_CODE(String pARTNER_CODE) {
        PARTNER_CODE = pARTNER_CODE;
    }

    public String getURL() {
        return URL;
    }

    public void setURL(String uRL) {
        URL = uRL;
    }

    public String getMD5_KEY() {
        return MD5_KEY;
    }

    public void setMD5_KEY(String mD5_KEY) {
        MD5_KEY = mD5_KEY;
    }

    /**
     * 生成sign
     * @param content
     * @return
     */
    public String createSign(String content){
        getConfigure();
        String sign = null;
        try {
            System.out.println("MD5_KEY:" + MD5_KEY);
            System.out.println("content:" + content);
            sign = DigestUtils.md5Hex(MD5_KEY + content);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("生成sign失败");
        }
        return sign;
    }

    /**
     * 生成响应xml
     * @param xmlMessageDTO
     * @return
     */
    public String callbackResponse(XmlMessageDTO xmlMessageDTO){
        return XmlMessageUtil.createXml(xmlMessageDTO.getPartnerCode(), xmlMessageDTO.getSign(), xmlMessageDTO.getContent());
    }

    /**
     * xml转xmlToJSON
     * @param xmlMessageStr
     * @return
     */
    public String xmlToJSON(String xmlMessageStr){
        String resultStr = null;
        try {
            if(xmlMessageStr != null && !"".equals(xmlMessageStr)){
                resultStr = XmlUtil.xml2JSON(xmlMessageStr).toJSONString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultStr;
    }

    /**
     * 获取字典表value
     * @param key
     * @return
     */
    public String getValue(String key){
        TMitServiceDicBase baseInfo = baseService.selectById(key);
        if (baseInfo == null){
            return null;
        }
        return baseInfo.getBvalue();
    }
}
